package com.github.ginirohikocha.dbm.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.github.ginirohikocha.dbm.R;
import com.github.ginirohikocha.dbm.activity.OffersActivity;
import com.github.ginirohikocha.dbm.entity.Offer;
import com.github.ginirohikocha.dbm.entity.ProductsPrice;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class RecycleProductsAdapter extends RecyclerView.Adapter<RecycleProductsAdapter.ProductHolder>  {
    //上下文
    private OffersActivity activity;
    //数据列表
    private List<Offer> offers = new ArrayList<>();

    private String orderNum;

    public RecycleProductsAdapter(Context context, List<Offer> offers, String orderNum) {
        this.activity = (OffersActivity)context;
        if (offers != null)
            this.offers = offers;
        this.orderNum = orderNum;
    }

    @NonNull
    @Override
    public ProductHolder  onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_product , viewGroup , false);
        return new ProductHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ProductHolder productHolder, int i) {
        productHolder.offer = offers.get(i);
        productHolder.singlePrice = productHolder.offer.getOfferPrice().doubleValue()/productHolder.offer.getOfferAmount();
        productHolder.title.setText(productHolder.offer.getOfferTitle());
        productHolder.subtitle.setText(productHolder.offer.getOfferDescription());
        productHolder.price.setText(String.format(Locale.CHINESE, "¥%.2f", productHolder.singlePrice));
        productHolder.amount.setVisibility(View.VISIBLE);
        productHolder.reduce.setVisibility(View.VISIBLE);
        productHolder.amount.setText(String.valueOf(productHolder.offer.getOfferAmount()));

        productHolder.add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                productHolder.addAmount();
            }
        });
        productHolder.reduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) { productHolder.reduceAmount();
            }
        });
    }

    @Override
    public int getItemCount() {
        return offers.size();
    }

    public double getPriceCount() {
        double price = 0;
        for (Offer offer:offers) {
            price += offer.getOfferPrice().doubleValue();
        }
        return price;
    }

    public void refresh(List<Offer> offers) {
        if (offers != null)
            this.offers = offers;
        notifyDataSetChanged();
    }

    /**
     * 单元Holder
     * **/
    class ProductHolder extends RecyclerView.ViewHolder {
        //布局组件
        private TextView title, subtitle, price, amount;
        private Button add, reduce;
        //订单内容
        private double singlePrice;
        private Offer offer;

        public ProductHolder(final View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.item_order_orderTitle);//产品标题
            subtitle = itemView.findViewById(R.id.item_order_orderSubtitle);//产品副标题
            price = itemView.findViewById(R.id.item_product_single_price);//产品单价
            amount = itemView.findViewById(R.id.item_product_amount);//选购数量
            add = itemView.findViewById(R.id.item_product_add);//添加产品
            reduce = itemView.findViewById(R.id.item_product_reduce);//减少产品
        }

        private void addAmount() {
            if (offer == null) {
                amount.setVisibility(View.VISIBLE);
                reduce.setVisibility(View.VISIBLE);
                offer = new Offer(null, orderNum, 1, title.getText().toString(), subtitle.getText().toString(), null, BigDecimal.valueOf(singlePrice), "未完成");
                offers.add(offer);
            } else {
                offer.setOfferAmount(offer.getOfferAmount()+1);
                offer.setOfferPrice(BigDecimal.valueOf(offer.getOfferAmount()*singlePrice));
            }
            amount.setText(String.valueOf(offer.getOfferAmount()));
            activity.refresh();
        }

        private void reduceAmount() {
            offer.setOfferAmount(offer.getOfferAmount()-1);
            if (offer.getOfferAmount() == 0) {
                amount.setVisibility(View.INVISIBLE);
                reduce.setVisibility(View.INVISIBLE);

                for (int i=0;i<offers.size();i++) {
                    if (offer.getId() == offers.get(i).getId()) {
                        offers.remove(i);
                        break;
                    }
                }
                offer = null;
            } else {
                amount.setText(String.valueOf(offer.getOfferAmount()));
                offer.setOfferPrice(BigDecimal.valueOf(offer.getOfferAmount()*singlePrice));
            }
            activity.refresh();
        }
    }
}
